package com.tadpole.music.presenter.me;

import com.tadpole.music.bean.me.ZhenTiSearchBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.ZhenTiSearchIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 真题
 * Created By 孙岩
 * on 2020/12/29
 */
public class ZhenTiSearchPresenter extends BasePresenter<ZhenTiSearchIView> {
    /**
     * @param user_id    学生id
     * @param rank_id    分类id
     * @param start_time 开始时间
     * @param end_time   结束时间
     * @param flag       1表示第一次进来 2表示筛选
     */
    @SuppressWarnings("unchecked")
    public void getZhenTi(String user_id, String rank_id, String start_time, String end_time, final String flag) {
        if (isViewAttached()) {
            String url = "";
            if (flag.equals("1")) {
                url = UrlConfig.getZhenTiList +"user_id="+ user_id + "&rank_id=" + rank_id;
            } else if (flag.equals("2")) {
                url = UrlConfig.getZhenTiList +"user_id="+ user_id + "&rank_id=" + rank_id + "&start_time=" + start_time + "&end_time=" + end_time;
            }else if(flag.equals("3")){
                url = UrlConfig.getZhenTiList +"user_id="+ user_id + "&rank_id=" + rank_id + "&start_time=" + start_time + "&end_time=" + end_time;
            }

            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    JSONObject monthDataObj = new JSONObject(object.getString("month_data"));
                                    List<Integer> dayList =new ArrayList<>();
                                    JSONArray dayArray = new JSONArray(monthDataObj.getString("days"));
                                    for(int i=0;i<dayArray.length();i++){
                                        dayList.add(dayArray.getInt(i));
                                    }
                                    ZhenTiSearchBean.DataBean.MonthDataBean monthDataBean = new ZhenTiSearchBean.DataBean.MonthDataBean(
                                            JsonParseUtil.getStr(monthDataObj,"month"),
                                            dayList
                                    );

                                    JSONObject staticObj = new JSONObject(object.getString("month_data_static"));
                                    ZhenTiSearchBean.DataBean.MonthDataStaticBean staticBean = new ZhenTiSearchBean.DataBean.MonthDataStaticBean(
                                        JsonParseUtil.getInt(staticObj,"total"),
                                        JsonParseUtil.getDouble(staticObj,"avg_time"),
                                        JsonParseUtil.getInt(staticObj,"sum_err_num"),
                                        JsonParseUtil.getInt(staticObj,"avg_score")
                                    );

                                    List<ZhenTiSearchBean.DataBean.MonthExamStaticBean> staticBeanList = new ArrayList<>();
                                    JSONArray staticArray = new JSONArray(object.getString("month_exam_static"));
                                    for(int j=0;j<staticArray.length();j++){
                                        JSONObject staticObject = new JSONObject(staticArray.get(j).toString());

                                        ZhenTiSearchBean.DataBean.MonthExamStaticBean examStaticBean = new ZhenTiSearchBean.DataBean.MonthExamStaticBean(
                                                JsonParseUtil.getInt(staticObject,"id"),
                                                JsonParseUtil.getInt(staticObject,"score"),
                                                JsonParseUtil.getInt(staticObject,"err_num"),
                                                JsonParseUtil.getInt(staticObject,"time"),
                                                JsonParseUtil.getStr(staticObject,"created_at")
                                        );
                                        staticBeanList.add(examStaticBean);
                                    }

                                    JSONObject totalObJ = new JSONObject(object.getString("total"));
                                    ZhenTiSearchBean.DataBean.TotalBean totalBean = new ZhenTiSearchBean.DataBean.TotalBean(
                                            JsonParseUtil.getInt(totalObJ,"total"),
                                            JsonParseUtil.getDouble(totalObJ,"avg_time"),
                                            JsonParseUtil.getInt(totalObJ,"sum_err_num"),
                                            JsonParseUtil.getInt(totalObJ,"avg_score")
                                    );

                                    ZhenTiSearchBean.DataBean bean = new ZhenTiSearchBean.DataBean(
                                            monthDataBean,
                                            staticBean,
                                            totalBean,
                                            staticBeanList
                                    );
                                    getView().showZhenTi(bean,flag);

                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
